using SmartCommunity.Models;
using Mapster;
using System.ComponentModel.DataAnnotations;

namespace SmartCommunity.WebApi.Services.System
{
    #region DTO属性映射
    /// <summary>
    /// 属性映射 MenuDtoRegister
    /// </summary>
    public class MenuDtoRegister : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<MenuEntity, MenuDto>().Map(d => d.Children, s => s.Children.Select(t => t.Adapt<MenuDto>()), s => s.Children.Count > 0);
        }
    }
    #endregion

    #region 数据传输对象 MenuDto
    /// <summary>
    /// 数据传输对象：菜单
    /// </summary>
    public class MenuDto : BaseDto
    {
        /// <summary>
        /// 菜单ID
        /// </summary>
        public int MenuId { get; set; }

        /// <summary>
        /// 上级ID
        /// </summary>
        public int? ParentId { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        [Required(ErrorMessage = "菜单名称不能为空")]
        [StringLength(50, ErrorMessage = "菜单名称 长度不能超过 50.")]
        public string MenuName { get; set; } = string.Empty;

        /// <summary>
        /// 菜单类型
        /// </summary>
        public MenuType MenuType { get; set; }

        /// <summary>
        /// 菜单地址
        /// </summary>
        [Required(ErrorMessage = "菜单地址不能为空")]
        [StringLength(200, ErrorMessage = "菜单地址 长度不能超过 200.")]
        public string MenuPath { get; set; } = string.Empty;

        /// <summary>
        /// 排序号
        /// </summary>
        public int SortNo { get; set; }

        /// <summary>
        /// 权限标识
        /// </summary>
        [StringLength(200, ErrorMessage = "权限标识 长度不能超过 200.")]
        public string? Permission { get; set; }

        /// <summary>
        /// 菜单图标
        /// </summary>
        [StringLength(100, ErrorMessage = "菜单图标 长度不能超过 100.")]
        public string? Icon { get; set; }

        /// <summary>
        /// 备注说明
        /// </summary>
        [StringLength(255, ErrorMessage = "备注说明 长度不能超过 255.")]
        public string? Remark { get; set; }

        /// <summary>
        /// 下级菜单
        /// </summary>
        public List<MenuDto>? Children { get; set; }
    }
    #endregion

    #region 数据查询对象
    /// <summary>
    /// 查询模型：菜单
    /// </summary>
    public class MenuQuery : AbstractQuery<MenuEntity>
    {
        /// <summary>
        /// 主键ID多选查询
        /// </summary>
        public IEnumerable<int> Ids { get; set; } = [];

        /// <summary>
        /// 上级菜单ID
        /// </summary>
        public int? ParentId { get; set; }

        /// <summary>
        /// 查询层次的最小维度，默认查到按钮(3),查所有数据
        /// 如果传入菜单(2)，则只查模块和菜单，不查按钮，<br/>
        /// 如果传入模块(1)，则只查模块，不查菜单和按钮<br/>
        /// </summary>
        public int? MenuLevel { get; set; }

        /// <summary>
        /// 菜单类型
        /// </summary>
        public int? MenuType { get; set; }

        /// <summary>
        /// 根据查询条件添加查询表达式
        /// </summary>
        protected override void MergeQueryExpression()
        {
            AddExpression(Ids.Any(), e => Ids.Contains(e.MenuId));
            AddExpression(ParentId.HasValue, a => a.ParentId == ParentId);
            AddExpression(MenuLevel.HasValue, a => ((int)a.MenuType) <= MenuLevel!.Value);
            AddExpression(MenuType.HasValue, m => ((int)m.MenuType) == MenuType!.Value);
            AddSearchExpression(s => o => o.MenuName.Contains(s) || o.MenuPath.Contains(s) || o.Permission.Contains(s));
            AddDateExpression(start => e => e.CreatedTime >= start, end => e => e.CreatedTime <= end);
        }
    }
    #endregion

}
